package com.javacoo.limit.client.util;

import javax.servlet.http.HttpServletRequest;

import org.omg.CORBA.UNKNOWN;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 工具类
 * <li></li>
 *
 * @author: duanyong@jccfc.com
 * @since: 2021/5/23 15:58
 */
public class WebUtil {
    private static final String UNKNOWN = "unknown";
    /**
     * 获取IP
     * <p>说明:</p>
     * <li></li>
     * @author duanyong
     * @date 2021/5/7 22:35
     */
    public static String getIpAddress() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
